package game.editer.ui;

import hx.display.ImageLoader;
import hx.display.ArrayCollection;
import hx.layout.AnchorLayout;
import hx.layout.AnchorLayoutData;
import hx.display.Label;
import hx.events.MouseEvent;
import hx.display.Quad;
import hx.display.Box;

/**
 * 按钮
 */
class Button extends Box {
	public var upQuad:Quad;

	public var label:Label;

	public var enableIcon(default, set):Bool = false;

	public var icon = new ImageLoader("UIAtlas:menu");

	private function set_enableIcon(value:Bool):Bool {
		if (value) {
			icon.visible = true;
			cast(label.layoutData, AnchorLayoutData).right = 16;
		} else {
			icon.visible = false;
			cast(label.layoutData, AnchorLayoutData).right = 0;
		}
		return value;
	}

	public var text(default, set):String = null;

	private function set_text(value:String):String {
		this.text = value;
		label.data = value;
		return value;
	}

	public function new() {
		super();
		var w = 120;
		var h = 30;

		var lineQuad = new Quad(w, h, 0x2e2e2e);
		this.addChild(lineQuad);
		lineQuad.layoutData = AnchorLayoutData.fill(0);

		upQuad = new Quad(w, h, 0x585858);
		this.addChild(upQuad);
		upQuad.layoutData = AnchorLayoutData.fill(1);
		// overQuad = new Quad(w, h, 0x626262);
		// this.addChild(overQuad);
		// downQuad = new Quad(w, h, 0x556976);
		// this.addChild(downQuad);
		label = new Label("请选择", Config.COMMON_TEXT_FORMAT);
		this.addChild(label);
		this.layout = new AnchorLayout();
		label.layoutData = AnchorLayoutData.fill(0);
		cast(label.layoutData, AnchorLayoutData).right = 0;
		label.horizontalAlign = CENTER;
		label.verticalAlign = MIDDLE;
		label.mouseEnabled = false;
		this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		this.mouseChildren = false;

		this.addChild(icon);
		icon.layoutData = AnchorLayoutData.middleRight(0, 5);
		icon.width = 12;
		icon.height = 12;
		icon.visible = false;
	}

	override function onAddToStage() {
		super.onAddToStage();
		this.stage.addEventListener(MouseEvent.CLICK, onStageMouseUp);
	}

	override function onRemoveToStage() {
		super.onRemoveToStage();
		this.stage.removeEventListener(MouseEvent.CLICK, onStageMouseUp);
	}

	private function onStageMouseUp(event:MouseEvent):Void {
		__down = false;
	}

	private function onMouseOver(event:MouseEvent):Void {
		upQuad.data = 0x757575;
	}

	private function onMouseOut(event:MouseEvent):Void {
		upQuad.data = 0x585858;
	}

	public var data:ArrayCollection;

	private function onMouseUp(event:MouseEvent):Void {
		upQuad.data = 0x585858;
	}

	private var __down = false;

	private function onMouseDown(event:MouseEvent):Void {
		upQuad.data = 0x556976;
		__down = true;
	}
}
